package chapter1;

import java.util.Iterator;

public class LinkedStack<T> implements Stack<T> {
    private int size;
    private Node<T> head;

    public static void main(String[] args) {
        String s = "abcdefghijk";
        Stack<String> linkedStack= new LinkedStack<>();
        Stack<String> arrayStack = new ArrayStack<>();
        for (char c : s.toCharArray()) {
            linkedStack.push(String.valueOf(c));
            arrayStack.push(String.valueOf(c));
        }

        for (String a : linkedStack) {
            System.out.print(a);
        }

        System.out.println();

        for (String b : arrayStack) {
            System.out.print(b);
        }
    }

    @Override
    public void push(T t) {
        Node<T> old = head;
        head = new Node<>();
        head.setItem(t);
        head.setNext(old);
        size++;
    }

    @Override
    public T pop() {
        if (isEmpty()) {
            return null;
        }
        T item = head.getItem();
        head = head.getNext();
        size--;
        return item;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public Iterator<T> iterator() {
        return new LinkedIterator(head);
    }

}
